package shangji;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

public class Shangji_1 {
    public static void main(String[] args) throws Exception{
        //得到DOM解析器实例
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        //从DOM工厂获得DOM解析器
        DocumentBuilder db = dbf.newDocumentBuilder();
        //解析xml文档,得到一个Document对象,即DOM树
        Document doc = db.parse("src/收藏信息.xml");
        //创建Brand节点
        NodeList brand = doc.getElementsByTagName("Brand");
        //循环Brand信息
        for (int i = 0; i < brand.getLength(); i++) {
            Node item = brand.item(i);
            Element item1 = (Element) item;
            //Brand名称
            String name = item1.getAttribute("name");
            NodeList childNodes = item1.getChildNodes();
            for (int j = 0; j < childNodes.getLength(); j++) {
                Node item2 = childNodes.item(j);
                if (item2.getNodeType() == Node.ELEMENT_NODE) {
                    Element item21 = (Element) item2;
                    String name1 = item21.getAttribute("name");
                    System.out.println(name + ":" + name1);
                }
            }
        }
    }

}
